﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchoolLibrary.Entity.Person;

namespace SchoolLibrary.Entity
{
    public class Book : Identity
    {
        public const string TABLE_NAME = "book";

        public const string NAME_COLUMN_NAME = "name";
        public const string YEAR_COLUMN_NAME = "published_year";
        public const string PRICE_COLUMN_NAME = "price";
        public const string AMOUNT_COLUMN_NAME = "amount";
        public const string GENRE_ID_COLUMN_NAME = "genre_id";
        private const char SPACE_SUMBOL = ' ';
        private const char COMMA_SUMBOL = ',';

        public const string FIND_BOOKS = "SELECT * FROM book";
        public const string FIND_BOOK_NAME = "SELECT * FROM book where name like '%:name%'";
        public const string FIND_BY_ID = "SELECT * FROM book b WHERE b.id = :id";
        public const string SAVE = "INSERT INTO book (name, published_year, price, amount, genre_id) " +
            "VALUES(:name, :year, :price, :amount, :genreId)";
        public const string UPDATE = "UPDATE book SET name = name, published_year = :year, price = :price, " +
            "amount = :amount, genre_id = :genreId where id = :id";
        public const string DELETE_WRITERS_BY_BOOK_ID = "DELETE from book_writer WHERE book_id = :id";
        public const string ADD_WRITER_TO_BOOK = "INSERT INTO book_writer (book_id, writer_id) VALUES(:bookId, :writerId)";

        private string name;
        private List<Writer> writers;
        private Genre genre;      
        private int year;
        private int price;
        private int amount;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Price
        {
            get { return price; }
            set { price = value; }
        }

        public int Year
        {
            get { return year; }
            set { year = value; }
        }        

        public List<Writer> Writers
        {
            get { return writers; }
            set { writers = value; }
        }

        public Genre Genre
        {
            get { return genre; }
            set { genre = value; }
        }

        public int Amount
        {
            get { return amount; }
            set { amount = value; }
        }

        public string Authors
        {
            get
            {
                StringBuilder authors = new StringBuilder();
                for (int i = 0; i < writers.Count; i++)
                {
                    Writer writer = writers[i];
                    authors.Append(writer.AbbreviatedName);
                    if (i < writers.Count - 1)
                    {
                        authors.Append(COMMA_SUMBOL);
                        authors.Append(SPACE_SUMBOL);
                    }
                }
                return authors.ToString();
            }
        }

        public String GenreName
        {
            get { return genre.Name; }            
        }

        public override string ToString()
        {
            return name;
        }
    }
}
